<?php
/** 
 * EGP Framework
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */

!defined('EGP_LIB_DIR') && die('Access Deny!');

/**
 * 支付方式抽象类
 *
 * @package    classes
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */
abstract class EGP_Payment_Adapter_Abstract
{

    /**
     * 支付方式参数
     *
     * @var array
     */
    protected $_config = array();

    /**
     * 错误信息
     *
     * @var string
     */
    protected $_message = null;

    /**
     * 参数 HASH 值
     *
     * @var string
     */
    protected $_hash = null;

    /**
     * 是否已执行过校验
     *
     * @var boolean
     */
    protected $_isValid = false;

    /**
     * 参数校验结果
     *
     * @var boolean
     */
    protected $_validate = false;

    /**
     * 构造方法
     *
     * @param array $config
     */
    public function __construct(array $config = array())
    {
        $this->set($config);
    }

    /**
     * 设置参数
     *
     * @param  string  $name
     * @param  mixed   $value
     * @return EGP_Payment_Adapter_Abstract
     * @throws EGP_Exception
     */
    public function set($name, $value = null)
    {
        if (is_array($name) || $name instanceof EGP_ArrayObject) {
            foreach ($name as $key => $value) {
                $this->set($key, $value);
            }
        } else {
            if (!array_key_exists($name, $this->_config)) {
                throw new EGP_Exception("不存在的参数 '$name'");
            }
            $this->_config[$name] = $value;
        }

        return $this;
    }

    /**
     * 魔术方法 - 设置参数
     *
     * @param  string  $name
     * @param  mixed   $value
     */
    public function __set($name, $value)
    {
        $this->set($name, $value);
    }

    /**
     * 对参数进行校验
     *
     * @return boolean
     */
    public function isValid()
    {
        $hash = md5(implode('', array_values($this->_config)));
        if (!$this->_isValid && $this->_hash !== $hash) {
            try {
                foreach ($this->_config as $name => $value) {
                    $method = 'set' . implode('', array_map('ucfirst', explode('_', $name)));
                    if (!method_exists($this, $method)) {
                        throw new EGP_Exception("参数 '$name' 无法设置");
                    }
                    $this->$method($value);
                }
                $this->_validate = true;
            } catch (Exception $e) {
                $this->_message = (isset($name) ? $name . ': ' : '') . $e->getMessage();
                $this->_validate = false;
            }
            $this->_isValid = true;
            $this->_hash = $hash;
        }
        return $this->_validate;
    }

    /**
     * 获取错误信息
     *
     * @return string
     */
    public function getMessage()
    {
        return $this->_message;
    }

    /**
     * 是否有效的浮点数
     *
     * @param  float  $value
     * @return boolean
     */
    protected function _isFloat($value)
    {
        $locale = localeconv();
        $filtered = str_replace($locale['thousands_sep'], '', $value);
        $filtered = str_replace($locale['decimal_point'], '.', $filtered);
        return (strval(floatval($filtered)) == $filtered);
    }

    /**
     * 是否有效的 URL 地址
     *
     * @param  string  $url
     * @return boolean
     */
    protected function _isUrl($url)
    {
        return preg_match('/^https?:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"])*$/', $url);
    }

    /**
     * 是否有效的 IP 地址
     *
     * @param  string  $ip
     * @return boolean
     */
    protected function _isIp($ip)
    {
        $result = ip2long($ip);
        return ($result == -1 || $result === false) ? false : true;
    }

    /**
     * 是否有效的邮政编码
     *
     * @param  string  $zip
     * @return boolean
     */
    protected function _isZip($zip)
    {
        return preg_match('/^\d{6}$/', $zip);
    }

    /**
     * 是否有效的电话号码
     *
     * @param  string  $phone
     * @return boolean
     */
    protected function _isPhone($phone)
    {
        return preg_match('/^\+?[\d\-]{5,30}$/', $phone);
    }

    /**
     * 是否有效的手机号码
     *
     * @param  string  $mobile
     * @return boolean
     */
    protected function _isMobile($mobile)
    {
        return preg_match('/^\d{11}$/', $mobile);
    }

    /**
     * 是否有效的邮件地址
     *
     * @param  string  $mail
     * @return boolean
     */
    protected function _isMail($mail)
    {
        return preg_match('/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/', $mail);
    }

    /**
     * 设置订单编号
     *
     * @param  string  $id
     * @return EGP_Payment_Adapter_Abstract
     */
    abstract public function setId($id);

    /**
     * 设置订单主题
     *
     * @param  string  $subject
     * @return EGP_Payment_Adapter_Abstract
     */
    abstract public function setSubject($subject);

    /**
     * 设置订单总金额
     *
     * @param  float $amount
     * @return EGP_Payment_Adapter_Abstract
     */
    abstract public function setAmount($amount);

    /**
     * 获取支付网关地址
     *
     * @return string
     */
    abstract public function getGateWay();

    /**
     * 获取支付 URL
     *
     * @return string
     */
    abstract public function getPayUrl();

}